﻿using RSSReader.Properties;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Threading;

namespace RSSReader
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private FileInfo persistenceFileInfo = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"Data\" + Settings.Default.PersistenceDataFileName);
        private FeedList feedList;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            if (Settings.Default.LoadPersistenceData)
            {
                this.feedList = new FeedList(persistenceFileInfo);
            }
            else
            {
                this.feedList = new FeedList();
            }

            MainWindow mainWindow = new MainWindow(feedList);
            mainWindow.Show();
        }

        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Process unhandled exception
            MessageBox.Show(e.Exception.Message);

            // Prevent default unhandled exception processing
            e.Handled = true;
        }

        protected override void OnExit(ExitEventArgs e)
        {
            FeedList.SavePersistenceData(persistenceFileInfo, feedList);

            base.OnExit(e);
        }
    }
}
